import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../common/index.dart';
import '../myPlant/widgets/plant_card.dart';
import 'index.dart';
import 'widgets/index.dart';

class PlantDetailPage extends GetView<PlantDetailController> {
  const PlantDetailPage({super.key});

  // 显示上传选项菜单
  void _show_upload_options(BuildContext context) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        padding: EdgeInsets.all(24.r),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(top: Radius.circular(20.r)),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              "Upload plant image",
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            SizedBox(height: 20.h),
            ListTile(
              leading: Icon(Icons.camera_alt, color: Color(0xFFCDFF5F)),
              title: Text("Take a photo"),
              onTap: () {
                Navigator.pop(context);
                controller.take_and_upload_photo();
              },
            ),
            ListTile(
              leading: Icon(Icons.photo_library, color: Color(0xFFCDFF5F)),
              title: Text("Choose from gallery"),
              onTap: () {
                Navigator.pop(context);
                controller.pick_and_upload_image();
              },
            ),
            SizedBox(height: 10.h),
          ],
        ),
      ),
    );
  }

  // 主视图
  Widget _buildView(BuildContext context) {
    return Stack(
      children: [
        Column(
          children: [
            SizedBox(height: 60.h),
            Row(
              children: [
                SizedBox(width: 24.w),
                InkWell(
                  onTap: () => RouteUtil.pop(),
                  child: Container(
                    height: 40.h,
                    width: 40.w,
                    padding: EdgeInsets.all(10.r),
                    decoration: BoxDecoration(
                      color: Color(0xFFCDFF5F),
                      borderRadius: BorderRadius.circular(10.r),
                    ),
                    child: SvgPicture.asset(
                      AssetsImages.back,
                      width: 20.w,
                      height: 20.h,
                    ),
                  ),
                ),
                Spacer(),
                Text(
                  "Maintenance records",
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w900,
                    color: Colors.white,
                  ),
                ),
                Spacer(),
                Container(
                  height: 40.h,
                  width: 40.w,
                  padding: EdgeInsets.all(10.r),
                  decoration: BoxDecoration(
                    color: Color(0xFFCDFF5F),
                    borderRadius: BorderRadius.circular(10.r),
                  ),
                  child: InkWell(
                    onTap: () => _show_upload_options(context),
                    child: SvgPicture.asset(
                      AssetsImages.camera,
                      width: 20.w,
                      height: 20.h,
                    ),
                  ),
                ),
                SizedBox(width: 24.w),
              ],
            ),
            SizedBox(height: 20.h),
            PlantCard(
              plant: controller.plant,
              card_color: Color(0xFFF2F2F2),
              onTap: () {},
            ).paddingOnly(left: 24.w, right: 24.w),

            SizedBox(height: 20.h),

            // 历史图片时间轴
            HistoryImageTimeline(),
          ],
        ),
        // 加载遮罩
        Obx(
          () => controller.is_loading.value
              ? Container(
                  width: double.infinity,
                  height: double.infinity,
                  color: Colors.black.withOpacity(0.5),
                  child: Center(
                    child: Container(
                      padding: EdgeInsets.all(20.r),
                      decoration: BoxDecoration(
                        color: Colors.black.withOpacity(0.7),
                        borderRadius: BorderRadius.circular(10.r),
                      ),
                      child: CircularProgressIndicator(
                        color: Color(0xFFCDFF5F),
                      ),
                    ),
                  ),
                )
              : SizedBox.shrink(),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlantDetailController>(
      id: "plant_detail",
      builder: (_) {
        return Scaffold(
          body: Container(
            width: double.infinity,
            height: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(AssetsImages.bg),
                fit: BoxFit.cover,
              ),
            ),
            child: SafeArea(
              top: false,
              bottom: false,
              child: _buildView(context),
            ),
          ),
        );
      },
    );
  }
}
